<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" >
      <el-form-item label="机构分类" prop="orgKindId">
        <el-select v-model="queryParams.orgKindId" placeholder="请选择机构分类">
          <el-option
            v-for="item in orgKindsOptions"
            :key="item.orgKindId"
            :label="item.name"
            :value="item.orgKindId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['college:orgKindRoles:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orgKindRolesList">
      <el-table-column label="编号" align="center" prop="orgKindRelRolesId" />
      <el-table-column label="机构分类" align="center" prop="orgKindId" :formatter="getOrgKindName"/>
      <el-table-column label="是否必选" align="center" prop="requiredOpt" :formatter="requiredOptFormat" />
      <el-table-column label="角色名称" align="center" prop="roleId" :formatter="getRoleName"/>
      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['college:orgKindRoles:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改机构分类可用角色对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机构分类" prop="orgKindId">
         <el-select v-model="form.orgKindId" placeholder="请选择机构分类">
           <el-option
             v-for="item in orgKindsOptions"
             :key="item.orgKindId"
             :label="item.name"
             :value="item.orgKindId"
           ></el-option>
         </el-select>
        </el-form-item>
        <el-form-item label="是否必选" prop="requiredOpt">
          <el-select v-model="form.requiredOpt" placeholder="请选择是否必选">
            <el-option
              v-for="dict in requiredOptOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="roleIds">
          <el-select v-model="form.roleIds" multiple placeholder="请选择" >
            <el-option
              v-for="item in rolesOptions"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrgKindRoles, getOrgKindRoles, delOrgKindRoles,
addOrgKindRoles, updateOrgKindRoles, exportOrgKindRoles } from "@/api/college/orgKindRoles";
import {listOrgKinds} from "@/api/college/orgKinds";
import {listRole} from "@/api/system/role";
export default {
  name: "OrgKindRoles",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机构分类可用角色表格数据
      orgKindRolesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否必选字典
      requiredOptOptions: [],
      //机构分类列表
      orgKindsOptions:[],
      //角色分类
      rolesOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgKindId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orgKindId: [
          { required: true, message: "分类ID不能为空", trigger: "blur" }
        ],
        requiredOpt: [
          { required: true, message: "是否必选不能为空", trigger: "change" }
        ],
        roleId: [
          { required: true, message: "角色ID不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getOrgKinds();
    this.getRoles();
    this.getList();
    this.getDicts("requiredOpt").then(response => {
      this.requiredOptOptions = response.data;
    });
  },
  methods: {
    /** 查询机构分类可用角色列表 */
    getList() {
      this.loading = true;
      listOrgKindRoles(this.queryParams).then(response => {
        this.orgKindRolesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getOrgKinds(){
      let orgKindsForm = {};
      orgKindsForm.pageNum = 1;
      orgKindsForm.pageSize = 1000;
      listOrgKinds(orgKindsForm).then(response =>{
        this.orgKindsOptions = response.rows;
      });
    },
    getRoles(){
      let roleForm = {};
      roleForm.pageNum = 1;
      roleForm.pageSize = 1000;
      listRole(roleForm).then(response =>{
        this.rolesOptions = response.rows;
      });
    },
    getRoleName(row, column){
      let roleId = row.roleId;
      let roleName = '';
      for(var i=0; i< this.rolesOptions.length; i++){
        if(this.rolesOptions[i].roleId == roleId){
          roleName =  this.rolesOptions[i].roleName;
          break;
        }
      }
      return roleName;
    },
    getOrgKindName(row, column){
      let orgKindId = row.orgKindId;
      let name = '';
      for(var i=0;i<this.orgKindsOptions.length;i++){
        if(this.orgKindsOptions[i].orgKindId==orgKindId){
          name = this.orgKindsOptions[i].name;
          break;
        }
      }
      return name;
    },
    // 是否必选字典翻译
    requiredOptFormat(row, column) {
      return this.selectDictLabel(this.requiredOptOptions, row.requiredOpt);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orgKindRelRolesId: null,
        orgKindId: null,
        requiredOpt: null,
        roleId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orgKindRelRolesId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机构分类可用角色";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orgKindRelRolesId = row.orgKindRelRolesId || this.ids
      getOrgKindRoles(orgKindRelRolesId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改机构分类可用角色";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orgKindRelRolesId != null) {
            updateOrgKindRoles(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrgKindRoles(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orgKindRelRolesIds = row.orgKindRelRolesId || this.ids;
      this.$confirm('是否确认删除机构分类可用角色编号为"' + orgKindRelRolesIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrgKindRoles(orgKindRelRolesIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有机构分类可用角色数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrgKindRoles(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
